const express = require('express')
const router = express.Router()
const dbManager = require('../db/db_manager')
const ErrorCode = require('../config/ErrorCode')

router.get('/', (req, res) => {
  res.send('/api/factor/ root ...')
})

router.post('/list', async (req, res) => {
  try {
    const data = await dbManager.getFactors()
    res.send(data)
  } catch (err) {
    res.json({
      Message: err.Message,
      ErrorCode: ErrorCode.Query.Unknown
    })
  }
})

// body的格式
// [
//   = 的情况     {fieldname:value}
//   like 的情况  {fieldname:{like:'表达式'}}
// ]

router.post('/id/:id', async (req, res) => {
  try {
    const data = await dbManager.getFactorsById(req.params.id)
    console.log(data)
    res.json(data)
  } catch (err) {
    res.json({
      Message: err.Message,
      ErrorCode: ErrorCode.Query.Unknown
    })
  }
})

module.exports = router
